<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员查询" prop="memberType">
        <el-input
          v-model="phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQueryByPhone"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryByPhone">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="font-size: 4px;color: darkgrey">第一步
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="userHandleAdd"
          v-hasPermi="['hotel:hlrecord:add']"
        >添加用户</el-button>
      </el-col>
      <el-col :span="1.5" style="font-size: 4px;color: darkgrey">第二步
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotel:hlrecord:add']"
        >办理会员</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hotel:hlrecord:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hlrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="user.name" />
      <el-table-column label="用户手机号" align="center" prop="user.phone" />
      <el-table-column label="会员类型" align="center" prop="member.memberType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.user_member_type" :value="scope.row.member.memberType"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="录入时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlelevelAdd(scope.row)"
            v-hasPermi="['hotel:hlrecord:edit']"
          >升级会员</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户新增对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="userForm" :model="userForm" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="userForm.name" placeholder="请输入姓名" style="width: 260px"/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="userForm.sex" placeholder="请选择性别" style="width: 260px">
            <el-option
              v-for="dict in dict.type.user_sex_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="籍贯" prop="origin">
          <el-input v-model="userForm.origin" type="textarea" placeholder="请输入籍贯" style="width: 260px"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="userForm.phone"  placeholder="请输入手机号" style="width: 260px"/>
        </el-form-item>
        <el-form-item label="证件类型" prop="cardId">
          <el-select v-model="userForm.cardId" placeholder="请选择证件类型" style="width: 260px">
            <el-option
              v-for="dict in dict.type.user_card_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号" prop="idCard">
          <el-input v-model="userForm.idCard" placeholder="请输入证件号" style="width: 260px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUserForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 办理会员对话框 -->
    <el-dialog :title="title" :visible.sync="ok" width="500px" append-to-body>
      <el-form ref="memberForm" :model="memberForm" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="memberForm.phone"  placeholder="请输入手机号" style="width: 260px"/>
        </el-form-item>
        <el-form-item label="会员类型" prop="memberType">
          <el-select v-model="memberForm.memberType" placeholder="请选择会员类型" style="width: 260px">
            <el-option
              v-for="dict in dict.type.user_member_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitMemberForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 升级会员对话框 -->
    <el-dialog :title="title" :visible.sync="level" width="500px" append-to-body>
      <el-form ref="memberForm" :model="memberForm" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="memberForm.phone" style="width: 260px"/>
        </el-form-item>
        <el-form-item label="会员类型" prop="memberType">
          <el-select v-model="memberForm.memberType" placeholder="请选择会员类型" style="width: 260px">
            <el-option
              v-for="dict in dict.type.user_member_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitLevelMemberForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  listHlrecord,
  delHlrecord,
  addHlrecordForUser,
  addHlrecordForMember,
  addHlrecordForLevelMember,
  listHlrecordByPhone
} from "@/api/hotel/hlrecord";

export default {
  name: "Hlrecord",
  dicts: ['user_member_type', 'user_member_checkout_room', 'user_member_breakfast','user_card_type','user_sex_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 临时tel
      tempTel:'',
      // 会员权限表格数据
      hlrecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示用户弹出层
      ok: false,
      // 是否显示升级弹出层
      level: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberType: null,
      },
      // tel
      phone: '',
      // 会员类型
      memberForm: {},
      // 表单参数
      form: {},
      // 用户表单参数
      userForm:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 查询会员权限列表 */
    getList() {
      this.loading = true;
      listHlrecord(this.queryParams).then(response => {
        this.hlrecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询会员权限列表根据手机号 */
    handleQueryByPhone(){
      if(this.phone ===''){
        this.getList()
      }else {
        this.loading = true;
        listHlrecordByPhone(this.phone).then(response => {
          this.hlrecordList = []
          this.hlrecordList = response.rows;
          this.loading = false;
          this.phone === '';
        })
      }

    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.ok = false;
      this.level = false;
      this.reset();
      this.resetUserForm();
      this.resetMemberForm();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        memberId: null,
        state: null,
        user: null,
        member: null,
        remark: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    // 用户表单重置
    resetUserForm(){
      this.userForm = {
        id: null,
        memberId: null,
        name: null,
        sex: null,
        origin: null,
        phone: null,
        idCard: null,
        cardId: null,
        activeTime: null,
        remark: null,
        createBy: null,
        updateBy: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("userForm");
    },
    // 会员表单重置
    resetMemberForm(){
      this.memberForm = {
        phone: null,
        memberType: null
      };
      this.resetForm("memberForm");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 用户新增按钮操作 */
    userHandleAdd() {
      this.reset();
      this.resetUserForm();
      this.resetMemberForm();
      this.open = true;
      this.title = "添加用户";
    },

    /** 办理会员按钮操作 */
    handleAdd() {
      this.reset();
      this.resetMemberForm();
      this.memberForm.phone = this.userForm.phone;
      this.resetUserForm();
      this.tempTel ='';
      this.ok = true;
      this.title = "办理会员";
    },

    /** 升级会员按钮操作 */
    handlelevelAdd(row) {
      this.reset();
      this.resetUserForm();
      this.resetMemberForm();
      this.tempTel ='';
      this.memberForm.phone = row.user.phone
      this.tempTel = row.user.phone
      this.level = true;
      this.title = "升级会员";
    },


    /** 添加用户提交按钮 */
    submitUserForm() {
      this.$refs["userForm"].validate(valid => {
        if (valid) {
          console.log("新增用户")
          console.log(this.userForm)
          this.tempTel = this.userForm.phone;
            addHlrecordForUser(this.userForm).then(response => {
              console.log(response)
              if("false"===response.msg){
                this.$modal.msgError(response.data);
                this.open = false;
              }else if("true"===response.msg){
                this.$modal.msgSuccess(response.data);
                this.open = false;

                listHlrecordByPhone(this.tempTel).then(response => {
                  this.hlrecordList = [];
                  this.tempTel = '';
                  this.hlrecordList = response.rows;
                  this.loading = false;
                })

            }
        });
      }})
    },

    /** 办理会员提交按钮 */
    submitMemberForm() {
      this.$refs["memberForm"].validate(valid => {
        if (valid) {
          console.log("办理会员")
          this.tempTel = this.memberForm.phone;
            addHlrecordForMember(this.memberForm).then(response => {

              if("false"===response.msg){
                this.$modal.msgError(response.data);
                this.ok = false;
              }else if("true"===response.msg){
                this.$modal.msgSuccess(response.data);
                this.ok = false;
                this.loading = true;
                // 通过当前用户的手机号查到这条记录
                listHlrecordByPhone(this.tempTel).then(response => {
                  this.hlrecordList = [];
                  this.tempTel = '';
                  this.hlrecordList = response.rows;
                  this.loading = false;
                })




              }

            });
        }})
      },

    /** 升级会员提交按钮 */
    submitLevelMemberForm() {
      this.$refs["memberForm"].validate(valid => {
        if (valid) {
          console.log("升级会员")
          this.tempTel = this.memberForm.phone;
            addHlrecordForLevelMember(this.memberForm).then(response => {

              if("false"===response.msg){
                this.$modal.msgError(response.data);
                this.level = false;
              }else if("true"===response.msg) {
                this.$modal.msgSuccess(response.data);
                this.level = false;
                this.loading = true;
                // 通过当前用户的手机号查到这条记录
                listHlrecordByPhone(this.tempTel).then(response => {
                  this.hlrecordList = [];
                  this.tempTel = '';
                  this.hlrecordList = response.rows;
                  this.loading = false;
                })

              }


            });
        }})
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('hotel/hlrecord/export', {
        ...this.queryParams
      }, `hlrecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
